<template>
    <div>
        <Card shadow>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page 
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber" 
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
        </Card>
        <Modal v-model="showAdd" width="700">
            <h3 slot="header" style="color:#2D8CF0">请填写以下信息</h3>
            <Row>
                <Col span="22" offset="1">
                    <Form ref="formItem" :model="formItem" :label-width="100">
                        <FormItem label="姓名：">
                            <Input v-model="formItem.name" placeholder=""></Input>
                        </FormItem>
                        <FormItem label="出生年月：">
                            <DatePicker type="date" placeholder="" style="width: 100%" @on-change="changeBirthday"></DatePicker>
                        </FormItem>
                        <FormItem label="性别：">
                            <Select v-model="formItem.sex" placeholder="">
                                <Option value="0">男</Option>
                                <Option value="1">女</Option>
                            </Select>
                        </FormItem>
                        <FormItem label="婚姻：">
                            <Select v-model="formItem.marital" placeholder="">
                                <Option value="0">已婚</Option>
                                <Option value="1">未婚</Option>
                            </Select>
                        </FormItem>
                        <FormItem label="职业：">
                            <Select v-model="formItem.job" placeholder="">
                                <Option value="0">公司职员</Option>
                                <Option value="1">运动员</Option>
                                <Option value="2">演艺业</Option>
                                <Option value="3">学生</Option>
                                <Option value="4">记者</Option>
                                <Option value="5">公务员</Option>
                                <Option value="6">其他</Option>
                            </Select>
                        </FormItem>
                        <FormItem label="文化程度：">
                            <Select v-model="formItem.culture" placeholder="">
                                <Option value="0">小学</Option>
                                <Option value="1">初中</Option>
                                <Option value="2">高中</Option>
                                <Option value="3">大学</Option>
                                <Option value="4">研究生</Option>
                                <Option value="5">博士生</Option>
                            </Select>
                        </FormItem>
                        <FormItem label="住址：">
                            <Input v-model="formItem.address" placeholder=""></Input>
                        </FormItem>
                        <FormItem label="手机号：">
                            <Input v-model="formItem.phone" placeholder=""></Input>
                        </FormItem>
                    </Form>
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title">
                            您是怎么知道展超丽格的
                        </p>
                        <CheckboxGroup v-model="source">
                            <Checkbox label="0">户外广告</Checkbox>
                            <Checkbox label="1">亲朋介绍</Checkbox>
                            <Checkbox label="2">医生介绍</Checkbox>
                            <Checkbox label="3">网络达人</Checkbox>
                            <Checkbox label="4">网络宣传</Checkbox>
                            <Checkbox label="5">网站</Checkbox>
                            <Checkbox label="6">微信</Checkbox>
                            <Checkbox label="7">微博</Checkbox>
                            <Checkbox label="8">媒体报导</Checkbox>
                            <Checkbox label="9">其他</Checkbox>
                        </CheckboxGroup>
                    </Card>
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title">
                            您通过怎样的交通工具到达展超丽格
                        </p>
                        <CheckboxGroup v-model="travel">
                            <Checkbox label="0">地铁</Checkbox>
                            <Checkbox label="1">公交</Checkbox>
                            <Checkbox label="2">驾车</Checkbox>
                            <Checkbox label="3">出租车</Checkbox>
                            <Checkbox label="4">徒步</Checkbox>
                            <Checkbox label="5">其他</Checkbox>
                        </CheckboxGroup>
                    </Card>
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title">
                            您希望展超丽格通过什么样的方式能给您带去更多的帮助
                        </p>
                        <CheckboxGroup v-model="suggest">
                            <Checkbox label="0">专题讲座</Checkbox>
                            <Checkbox label="1">微信科普知识推广</Checkbox>
                            <Checkbox label="2">微博科普知识推广</Checkbox>
                            <Checkbox label="3">网络互动问答</Checkbox>
                            <Checkbox label="4">经典案列分享</Checkbox>
                            <Checkbox label="5">其他</Checkbox>
                        </CheckboxGroup>
                    </Card>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="info" size="large" @click="save">保存</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            showAdd: false,
            baseURL: '/user/new/diagnosis',
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/new/diagnosis',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: 1705,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '姓名',
                        key: 'name'
                    },{
                        title: '出生年月',
                        key: 'birthday',
                        render: (h, params) => {
                            return h('span', this.$moment(params.row.birthday).format('YYYY-MM-DD'))
                        }
                    },{
                        title: '性别',
                        key: 'sex',
                        render: (h, params) => {
                            let temp = params.row.sex
                            if (temp === 0) {
                                temp = '男'
                            } else if (temp === 1) {
                                temp = '女'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '婚姻',
                        key: 'marital',
                        render: (h, params) => {
                            let temp = params.row.marital
                            if (temp === 0) {
                                temp = '已婚'
                            } else if (temp === 1) {
                                temp = '未婚'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '职业',
                        key: 'job',
                        render: (h, params) => {
                            let temp = params.row.job
                            if (temp === 0) {
                                temp = '公司职员'
                            } else if (temp === 1) {
                                temp = '运动员'
                            } else if (temp === 2) {
                                temp = '演艺业'
                            } else if (temp === 3) {
                                temp = '学生'
                            } else if (temp === 4) {
                                temp = '记者'
                            } else if (temp === 5) {
                                temp = '公务员'
                            } else if (temp === 6) {
                                temp = '其他'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '文化程度',
                        key: 'culture',
                        render: (h, params) => {
                            let temp = params.row.culture
                            if (temp === 0) {
                                temp = '小学'
                            } else if (temp === 1) {
                                temp = '初中'
                            } else if (temp === 2) {
                                temp = '高中'
                            } else if (temp === 3) {
                                temp = '大学'
                            } else if (temp === 4) {
                                temp = '研究生'
                            } else if (temp === 5) {
                                temp = '博士生'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '住址',
                        key: 'address'
                    },{
                        title: '手机号码',
                        key: 'phone'
                    },{
                        title: '患者来源',
                        key: 'source',
                        render: (h, params) => {
                            let temp = params.row.source
                            temp = temp.replace('[','').replace(']','')
                                .replace('0','户外广告')
                                .replace('1','亲朋介绍')
                                .replace('2','医生介绍')
                                .replace('3','网络达人')
                                .replace('4','网络宣传')
                                .replace('5','网站')
                                .replace('6','微信')
                                .replace('7','微博')
                                .replace('8','媒体报导')
                                .replace('9','其他')
                            return h('span', temp)
                        }
                    },{
                        title: '出行',
                        key: 'travel',
                        render: (h, params) => {
                            let temp = params.row.travel
                            temp = temp.replace('[','').replace(']','')
                                .replace('0','地铁')
                                .replace('1','公交')
                                .replace('2','驾车')
                                .replace('3','出租车')
                                .replace('4','徒步')
                                .replace('5','其他')
                            return h('span', temp)
                        }
                    },{
                        title: '患者建议',
                        key: 'suggest',
                        render: (h, params) => {
                            let temp = params.row.suggest
                            temp = temp.replace('[','').replace(']','')
                                .replace('0','专题讲座')
                                .replace('1','微信科普知识推广')
                                .replace('2','微博科普知识推广')
                                .replace('3','网络互动问答')
                                .replace('4','经典案列分享')
                                .replace('5','其他')
                            return h('span', temp)
                        }
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            },
            source: [],
            travel: [],
            suggest: [],
            formItem: {
                id: '',
                name: '',
                birthdayStr: '',
                sex: '',
                marital: '',
                job: '',
                culture: '',
                address: '',
                phone: '',
                source: '',
                travel: '',
                suggest: ''
            }
        };
    },
    methods: {
        events (item) {
            let path = item.path
            if (path === 'add') {
                this.add()
            } else if (path === 'edit') {
                this.edit()
            } else if (path === 'delete') {
                this.delete()
            }
        },
        add () {
            this.source = []
            this.travel = []
            this.suggest = []
            this.$bus.init(this.formItem)
            this.showAdd = true
        },
        save () {
            this.formItem.source = JSON.stringify(this.source)
            this.formItem.travel = JSON.stringify(this.travel)
            this.formItem.suggest = JSON.stringify(this.suggest)
            console.log(this.formItem)
            let url = this.baseURL + '/saveOrUpdate'
            this.$ajax.post(url, this.formItem).then(r  => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '操作成功')
                    this.showAdd = false
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                if (ids.length === 1) {
                    this.doDelete(ids)
                } else {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                }
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            });
        },
        refresh () {
            this.$refs.table.refresh()
        },
        changeBirthday (value, type) {
            this.formItem.birthdayStr = value
        },
        changeSelect (rows) {
            this.selectRows = rows
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/new') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
